#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
from typing import Annotated

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db.database import async_get_db
from app.core.pkg.log import logger
from app.schemes.prompts import UserPromptsModel
from app.util.common import build_question_prompt
from app.util.promqts import promqt_mapping
from app.core.exceptions.http_exceptions import NotFoundException
from app.services.user import user_service


prompts_router = APIRouter(prefix="/prompts", tags=["提示词语配置"])


@prompts_router.get("/list", description='获取所有提示词语信息')
async def get_all_prompts_configs():
    return {"success": True, 'data': promqt_mapping}


@prompts_router.get("/get", description=f'获取所有提示词语信息\n {" ".join(promqt_mapping.keys())}')
async def get_prompts(code: str):
    if code.strip() not in promqt_mapping.keys():
        logger.error("没有找到对应的提示词配置")
        raise NotFoundException("没有找到对应的提示词配置")
    return {"success": True, 'data': promqt_mapping.get(code.strip(), ""), "message": "success"}


@prompts_router.post("/user/prompts", description='获取用户绑定提示词语')
async def get_prompt(user_prompt_model: UserPromptsModel, db: Annotated[AsyncSession, Depends(async_get_db)]):
    logger.info(f"接收到 /get_prompt 请求，payload_id: {user_prompt_model.payload_id}，问题: {user_prompt_model.question}")
    user_info = await user_service.get(db=db, payload_id=user_prompt_model.payload_id)
    if not user_info:
        logger.error("未查到用户信息!")
        return {"success": True, 'data': {}, "message": "未查到用户信息!"}


    birthdate = user_info.get("birthday", "").replace("T", " ")
    prompt = build_question_prompt(birthdate=birthdate, sex=user_info.get("gender", ""), question=user_prompt_model.question)
    logger.info("/get_prompt 请求处理完成。获取到的 prompt")
    return {"success": True, 'data': prompt, "message": "success"}
